package com.itfreer.data.remote;

import com.itfreer.utils.config.ConfigManager;
import com.itfreer.utils.http.HttpUtils;
import com.itfreer.utils.volley.callback.VoidCallback;
import com.itfreer.utils.common.JSONUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 定义通用数据访问服务
 */
public class DataService {

    /**
     * 单一实例
     */
    public static final DataService instance = new DataService();

    private DataService() {

    }

    /**
     * 获取查询数据列表
     * @param sn                        服务名称
     * @param fields                    查询字段
     * @param where                     查询条件
     * @param order                     排序
     * @param pageSize                  分页大小
     * @param pageIndex                 分页索引
     */
    public <T> List<T> getEntitys(
            String sn,
            String fields,
            Map<String,Object> where,
            Map<String,Integer> order,
            int pageSize,
            int pageIndex,
            Class<T> entityClass) {
        try {
            HashMap<String, Object> params = new HashMap<>();
            params.put("queryFields", fields);
            if(where!=null){
                params.put("where", JSONUtils.toJSONString(where));
            }
            if(order!=null){
                params.put("order", JSONUtils.toJSONString(order));
            }
            params.put("pageSize", pageSize);
            params.put("pageIndex", pageIndex);

            String url = ConfigManager.getConfig("HttpUrl").toString();
            url += "/rest/" + sn + "/getEntitys";

            List<T> rusult = HttpUtils.postList(url, params, entityClass);
            return rusult;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 获取记录数
     *
     * @param sn               服务名称
     * @param where            查询条件
     */
    public Integer getRowCounts(
            String sn,
            Map<String,Object> where){
        try {
            HashMap<String, Object> params = new HashMap<>();
            if(where!=null){
                params.put("where", JSONUtils.toJSONString(where));
            }
            String url = ConfigManager.getConfig("HttpUrl").toString();
            url += "/rest/" + sn + "/getRowCounts";

            Integer count = HttpUtils.post(url, params, Integer.class);
            return count;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 获取实体类
     *
     * @param sn                        服务名称
     * @param id                        id值
     */
    public <T> T getEntity(
            String sn,
            String id,
            Class<T> entityClass){
        try {
            String url = ConfigManager.getConfig("HttpUrl").toString();
            url += "/rest/" + sn + "/getEntity/" + id;
            T row = HttpUtils.post(url, null, entityClass);
            return row;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 添加记录
     * @param sn
     * @param row
     */
    public <T> T add(
            String sn,
            T row,
            Class<T> entityClass){
        try {
            HashMap<String, Object> params = new HashMap<>();
            if(row!=null){
                params.put("entity", JSONUtils.toJSONString(row));
            }
            String url = ConfigManager.getConfig("HttpUrl").toString();
            url += "/rest/" + sn + "/add";

            T nrow = HttpUtils.post(url, params, entityClass);
            return nrow;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 添加记录
     * @param sn
     * @param row
     */
    public <T> T update(
            String sn,
            T row,
            Class<T> entityClass){
        try {
            HashMap<String, Object> params = new HashMap<>();
            if(row!=null){
                params.put("entity", JSONUtils.toJSONString(row));
            }
            String url = ConfigManager.getConfig("HttpUrl").toString();
            url += "/rest/" + sn + "/update";

            T nrow = HttpUtils.post(url, params, entityClass);
            return nrow;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 删除记录
     * @param sn
     * @param id
     * @param listener
     */
    public void delete(
            String sn,
            String id,
            VoidCallback listener){
        String url = ConfigManager.getConfig("HttpUrl").toString();
        url += "/rest/" + sn + "/delete/" + id;
        try {
            HttpUtils.delete(url, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除记录
     * @param sn
     * @param ids
     * @param listener
     */
    public void deletes(
            String sn,
            String ids,
            VoidCallback listener){
        String url = ConfigManager.getConfig("HttpUrl").toString();
        url += "/rest/" + sn + "/deletes/" + ids;
        try {
            HttpUtils.delete(url, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}